var date = new Date();
var calStartChecker = new Date();
var calEndChecker = new Date();
var nowYear;
var nowMonth;
var nowDate;
var month;
var maxCalNum = 12;

function createCalendar( ) {
	calEndChecker.setMonth(calEndChecker.getMonth() + maxCalNum - 1);

	var date = new Date();
	nowDate = date.getDate();
	nowMonth = date.getMonth() + 1;
	nowYear = date.getFullYear();
	month = nowMonth;

	createTopTable();

	for ( var h = 0; h < maxCalNum; h++) {
		if (h > 0) {
			month++;
		}
		date.setMonth(month, 0);
		var lastDate = date.getDate();
		date.setDate(1);
		var startDay = date.getDay();
		month = date.getMonth() + 1;

		var tableParent = document.createElement("div");
		tableParent.id = "cal" + date.getFullYear() + "_" + month;
		var table = document.createElement("table");
		table.id = "calTable";
		table.border = 1;
		table.width = 500;
		table.align = "center";

		var days = [ "일", "월", "화", "수", "목", "금", "토" ];
		var tr = document.createElement("tr");
		for ( var i = 0; i < 7; i++) {
			var td = document.createElement("td");
			td.innerHTML = days[i];
			tr.appendChild(td);
		}
		table.appendChild(tr);

		for ( var i = 1; i <= 6; i++) {
			var tr = document.createElement("tr");

			for ( var j = 1; j <= 7; j++) {
				var td = document.createElement("td");
				td.id = "day" + date.getFullYear() + "_" + month + "_" + (i * 7 - (7 - j));
				tr.appendChild(td);
			}
			table.appendChild(tr);
		}
		tableParent.appendChild(table);
		$("#calendar").append(tableParent);

		for ( var i = 0; i < 42; i++) {
			if (i <= startDay) {
				continue;
			} else if (i - startDay > lastDate) {
				break;
			}
			var hid = document.createElement("input");
			hid.type = "hidden";
			hid.name = "days";
			hid.value = date.getFullYear() + "-" + month + "-" + ( i - startDay );
			$("#day" + date.getFullYear() + "_" + month + "_" + i).html( i - startDay );
			$("#day" + date.getFullYear() + "_" + month + "_" + i).append( hid );
		}
		$("#cal" + date.getFullYear() + "_" + month).css("display", "none");
	}
	$("table[id=calTable]").attr("cellspacing", 0);
	$("table[id=calTable] > tr > td").attr("width", 30);
	$("table[id=calTable]").attr("bordercolor", "#cccccc");
	$("table[id=calTable] > tr").css("height", "64px");
	$("#calendar").css("border", "1px solid #aaaaaa");
	$("#cal" + nowYear + "_" + nowMonth).css("display", "block");
	date.setFullYear(nowYear, nowMonth - 1, nowDate);
}

function createTopTable() {
	var table = document.createElement("table");
	table.width = "300";
	table.align = "center";
	var tr = document.createElement("tr");
	var td1 = document.createElement("td");
	var td2 = document.createElement("td");
	var td3 = document.createElement("td");
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	table.appendChild(tr);

	var beforeBtn = document.createElement("input");
	var nextBtn = document.createElement("input");
	beforeBtn.type = "button";
	beforeBtn.value = "◀";
	beforeBtn.id = "beforeBtn";
	nextBtn.type = "button";
	nextBtn.value = "▶";
	nextBtn.id = "nextBtn";

	td1.appendChild(beforeBtn);
	td1.align = "center";
	td2.id = "dateLb";
	td2.align = "center";
	td2.innerHTML = date.getFullYear() + "년 " + (date.getMonth() + 1) + "월";
	td3.appendChild(nextBtn);
	td3.align = "center";

	$("#calendar").append(table);
	$("#beforeBtn").attr("onclick", "beforeMonth()");
	$("#nextBtn").attr("onclick", "nextMonth()");
}

function nextMonth() {
	if (calEndChecker.getFullYear() == date.getFullYear()
			&& calEndChecker.getMonth() == date.getMonth()) {
		alert("등록 가능한 마지막 달 입니다.");
		return;
	}
	$("#cal" + date.getFullYear() + "_" + (date.getMonth() + 1)).css("display", "none");
	date.setMonth(date.getMonth() + 1, 1);
	$("#dateLb").html(date.getFullYear() + "년 " + (date.getMonth() + 1) + "월");
	$("#cal" + date.getFullYear() + "_" + (date.getMonth() + 1)).css("display", "block");
}

function beforeMonth() {
	if (calStartChecker.getFullYear() == date.getFullYear()	&& calStartChecker.getMonth() == date.getMonth()) {
		alert("등록 가능한 첫번째 달 입니다.");
		return;
	}
	$("#cal" + date.getFullYear() + "_" + (date.getMonth() + 1)).css("display", "none");
	date.setMonth(date.getMonth() - 1, 1);
	$("#dateLb").html(date.getFullYear() + "년 " + (date.getMonth() + 1) + "월");
	$("#cal" + date.getFullYear() + "_" + (date.getMonth() + 1)).css("display", "block");
}